//
//  AppDelegate.m
//  digitalCurrency
//
//  Created by sunliang on 2018/1/26.
//  Copyright © 2018年 XinHuoKeJi. All rights reserved.
//

#import "AppDelegate.h"
#import "YLNavigationController.h"
#import "YLTabBarController.h"
#import "CTLoginVC.h"

#import <UserNotifications/UserNotifications.h>
#import <CoreTelephony/CTCellularData.h>
#import "AppDelegate+Settings.h"
#import "XHLaunchAd.h"
@interface AppDelegate ()</*chatSocketDelegate,*/UNUserNotificationCenterDelegate> {
    NSString*_deviceToken;
}

@end

@implementation AppDelegate

+ (instancetype)sharedAppDelegate
{
    return (AppDelegate *)[UIApplication sharedApplication].delegate;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // 启动图片延时: 1秒
//    [NSThread sleepForTimeInterval:1];
    
    //[self getHost];
    
    //初始化应用语言
    [ChangeLanguage initUserLanguage];
    //[ChangeLanguage setUserlanguage:[ChangeLanguage userLanguage]];
    
    [self initKeyboardManager];//初始化键盘工具
    [CSToastManager setQueueEnabled:NO];

    [EasyShowOptions sharedEasyShowOptions].lodingShowType = LodingShowTypeTurnAroundLeft;
    
//    #if defined(DEBUG)||defined(_DEBUG)
//        [NEHTTPEye setEnabled:YES];
//    #endif
//    [self connectSocket];
    
    [self initMob];
    
//    [self setQiDongView];
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor=[UIColor whiteColor];
   
//    [self initGuaidView];
    //判断用户是否已经登录
   
    if (![YLUserInfo isLogIn]) {
        CTLoginVC *login = [[CTLoginVC alloc] init];
        YLNavigationController *nav = [[YLNavigationController alloc] initWithRootViewController:login];
        self.window.rootViewController = nav;
    }
    else {
        YLTabBarController *SectionTabbar = [[YLTabBarController alloc] init];
        self.window.rootViewController = SectionTabbar;
    }

//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        [self PresentGestureLockViewController];
//    });

    [self.window makeKeyAndVisible];
//    if ([self isFirstLauch]) {
//
//    }
//    else {
//        //启动动画
//       // [self customLaunchImageView];
//    }
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
        
    return YES;
}

- (void)setQiDongView{
     XHLaunchImageAdConfiguration *imageAdconfiguration = [XHLaunchImageAdConfiguration defaultConfiguration];
           //广告图片URLString/或本地图片名(.jpg/.gif请带上后缀)
        imageAdconfiguration.imageNameOrURLString = @"qidong.gif";
        imageAdconfiguration.imageOption = XHLaunchAdImageDefault;
        //图片填充模式
        imageAdconfiguration.contentMode = UIViewContentModeScaleAspectFill;
        //广告点击打开链接
    //    imageAdconfiguration.openModel = @"";
        //广告显示完成动画
        imageAdconfiguration.showFinishAnimate =ShowFinishAnimateFadein;
        //跳过按钮类型
        imageAdconfiguration.skipButtonType = SkipTypeText;
        //后台返回时,是否显示广告
        imageAdconfiguration.showEnterForeground = NO;
        //显示开屏广告
        [XHLaunchAd imageAdWithImageAdConfiguration:imageAdconfiguration delegate:self];
}

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    if(self.isEable) {
        return UIInterfaceOrientationMaskLandscape;
    }
    else {
        return UIInterfaceOrientationMaskPortrait;
    }
}

// 获取当前活动的navigationcontroller
- (UINavigationController *)navigationViewController
{
    if ([self.window.rootViewController isKindOfClass:[UINavigationController class]])
    {
        return (UINavigationController *)self.window.rootViewController;
    }
    else if ([self.window.rootViewController isKindOfClass:[UITabBarController class]])
    {
        UIViewController *selectVc = [((UITabBarController *)self.window.rootViewController) selectedViewController];
        if ([selectVc isKindOfClass:[UINavigationController class]])
        {
            return (UINavigationController *)selectVc;
        }
    }
    return nil;
}

- (UIViewController *)topViewController
{
    UINavigationController *nav = [self navigationViewController];
    return nav.topViewController;
}

- (void)pushViewController:(UIViewController *)viewController
{
    @autoreleasepool
    {
        viewController.hidesBottomBarWhenPushed = YES;
        [[self navigationViewController] pushViewController:viewController animated:YES];
    }
}

- (void)pushViewController:(UIViewController *)viewController withBackTitle:(NSString *)title
{
    @autoreleasepool
    {
        viewController.hidesBottomBarWhenPushed = YES;
        [[self navigationViewController] pushViewController:viewController withBackTitle:title animated:YES];
    }
}

- (void)pushViewController:(UIViewController *)viewController withBackTitle:(NSString *)title backAction:(CommonVoidBlock)action
{
    @autoreleasepool
    {
        viewController.hidesBottomBarWhenPushed = YES;
        [[self navigationViewController] pushViewController:viewController withBackTitle:title action:action animated:NO];
    }
}

- (UIViewController *)popViewController
{
    return [[self navigationViewController] popViewControllerAnimated:YES];
}
- (NSArray *)popToRootViewController
{
    return [[self navigationViewController] popToRootViewControllerAnimated:YES];
}

- (NSArray *)popToViewController:(UIViewController *)viewController
{
    return [[self navigationViewController] popToViewController:viewController animated:YES];
}

- (void)presentViewController:(UIViewController *)vc animated:(BOOL)animated completion:(void (^)(void))completion
{
    UIViewController *top = [self topViewController];
    
    if (vc.navigationController == nil)
    {
        YLNavigationController *nav = [[YLNavigationController alloc] initWithRootViewController:vc];
        [top presentViewController:nav animated:animated completion:completion];
    }
    else {
        [top presentViewController:vc animated:animated completion:completion];
    }
}

- (void)dismissViewController:(UIViewController *)vc animated:(BOOL)animated completion:(void (^)(void))completion {
    if (vc.navigationController != [AppDelegate sharedAppDelegate].navigationViewController) {
        [vc dismissViewControllerAnimated:YES completion:nil];
    }
    else {
        [vc.navigationController popViewControllerAnimated:YES];
    }
}

- (void)applicationWillResignActive:(UIApplication *)application {
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
}

@end
